/*


Data 	: 
Folder 	: 
Date	: 2018-01-24

Creator		: Jonas Cederlof	(JC)
Description :	
Notes 		:

LATSEST UPDATE : 2018-11-22
*/

********************************************************************************
clear
set more		 off
cap   log close 	_all

log using 	"../log/A2_merged_jobb_firm_pers.log" 	, replace 
use 		"$datapath/A2_merged_RAMS_varsel.dta"


{ // Program Define (Fastmax & Fastmin)
********************************************************************************
*Description: 	The purpose of the program is to speed up the max function
*		and abstain from using the much slower egen (max) command.
capture program drop fastmax fastmin

program define fastmax, rclass
	syntax varlist [if] [in] , [by(varlist)] name(string)

	tempvar maxvar
	clonevar `maxvar' = `varlist'

	if  "`by'" != "" {
		bys `by' : 	replace `maxvar' = max( `maxvar'[_n-1], `maxvar') 
		bys `by' : 	gen  	`name'   =  `maxvar'[_N] 
	}
	else {
					replace `maxvar' = max( `maxvar'[_n-1], `maxvar') 
					gen  	`name'   = `maxvar'[_N] 
}
end

program define fastmin, rclass
	syntax varlist [if] [in] , [by(varlist)] name(string)

	tempvar maxvar
	clonevar `maxvar' = `varlist'

	if  "`by'" != "" {
		bys `by' : 	replace `maxvar' = min( `maxvar'[_n-1], `maxvar') 
		bys `by' : 	gen  	`name'   =  `maxvar'[_N] 
	}
	else {
					replace `maxvar' = min( `maxvar'[_n-1], `maxvar') 
					gen  	`name'   = `maxvar'[_N] 
}
end



}
*
{ // Dropping variabels that are of no use to us (at the moment)
*===============================================================================

*RAMS variabels
drop 	astsni* yrkstallnku  plantid lonfink

*Varsel variabels
drop 	varsel_postid komplettvarsel avslut_dat bekraft_dat 	///
	 jurform tr_dat   ///
	komplettvarsel varseltyp 
	
}
*	

*Replace firmid variable with the notifying firm
*Note: 	Earnings/wages and other variables have already been creted for other 
*	firms. So this could not have been done prior to this. The reason for
*	doing this is that we need to collapse by year/month. Hence a firmid
*	variable with different firms will become uninformative as indivuals can
*	hold several jobs. As well as lonfink for example. In order to know 
*	whether an indiviual is working at the notifying firm or antoher firm 
*	use ther "atfirm" dummy which is merged on below. For wages/earnings
*	and where they come from, I'm merging on this below.

bys lopnr : gen notifying_firm = firmid if nrdisp==dispid
fastmax notifying_firm, by(lopnr) name(NFid)
replace firmid = NFid
drop notifying_firm NFid

count
count if eventtime_def==0	


bys lopnr date : egen jobsinamonth= total(1) if work==1
replace jobsinamonth = 0 if jobsinamonth==. & year<2017



*Collapsing data such that lopnr and date are uniqe
*Note : All variabels are the same within lopnr and date expect astnr
order lopnr date persid
gcollapse (max) persid-jobsinamonth ,by(lopnr date)

count
count if eventtime_def==0	

*Note: 	All merges below are either non-firm specific (e.g earnings and wages
*	are created as being wages at other firm, all firms or notifying firm. 
*	If the merge is firm specficic we are always interesed in is the 
*	notifying firm (i.e the one reflected in firmid as replaced above) and
* 	hence matches on firmid can be done (see e.g when merging on tenure).



{ // Merging on covarites
*===============================================================================

{ // Merge on variabels from LISA 
*===============================================================================
*LISA 2000-2018
merge m:1 persid year using "$datapath/A0_LISAind_2000_2018.dta"
drop if _merge==2
drop 	_merge

}
*
{ // Merge on date of birth	
*===============================================================================
merge m:1 persid using "$datapath/A0_fodelsedatum.dta"

*Missing birth date for 513 people
lab var birth 	"Date of birth"

drop if _merge==2
drop  	_merge

}
*
{ // Merge on immigrant indicator
*===============================================================================
preserve
	use  "$rawdatapath/DS_Lev_Fodelseland.dta",clear

	destring fodgreg fodgrmor fodgrfar , replace

	*Generate: Immigrant and second generation immigrant dummy
	gen immigrant 		= fodgreg !=0
	gen immigrant_secgen  	= fodgrmor!=0 & fodgrfar!=0 & fodgreg==0	

	keep lopnr_personnr imm*
	duplicates drop _all, force 		
	
	rename lopnr_personnr persid
	
	tempfile immigrant
	save `immigrant'
restore	

*Merge onto data
merge m:1 persid using `immigrant'
drop if _merge==2
drop 	_merge
}
*
{ // Mere on gender 
*===============================================================================
*Note: Gender was also mergred from LISA data above but that restricts to people
*then present in LISA
drop female
preserve
	use  "$rawdatapath/gender.dta",clear
	rename lopnr persid
	rename woman female
	tempfile temp
	save `temp'
restore
	
merge m:1 persid using `temp'
drop if _merge==2
drop 	_merge

}
*
{ // Merge on tenure
*===============================================================================
*Merge on JC tenure meassure
merge m:1 persid firmid date 	using "$datapath/A2_tenure.dta"
drop if _merge==2
drop 	_merge

*_merge==1 : periods where indivual does not work at notifying firm
*_merge==2 : due to the using data containing tenure from all firms and master
*	     data has a firmid that is replaced by the notifying firm
*_merge==3 : perfect!

*Merge on DS tenure meassure
*merge m:1 persid firmid date 	using "../data_long/tenure_seim_JCver.dta"
*drop if _merge==2
*drop 	_merge
	



}
*

}
*	
{ // Merge on mainemployer ID and other employer firmids
*===============================================================================
merge m:1 lopnr year 	using "$datapath/mainemployer.dta", nogen

merge 1:1 lopnr date 	using "$datapath/otherfirm_firmids.dta" , nogen

}
*
{ // Merging on outcomes
*===============================================================================

{ // Employment status
*===============================================================================

*Note: 	_merge==2 comes from having dropped years prior to 2000 above.
merge 1:1 lopnr date 	using "$datapath/working.dta", keep(3) nogen

* Note: this merge is not done on firmid but atfirm is created only for
*	the notifying firm (see A2_gen_employment.do")
merge 1:1 lopnr date 	using "$datapath/atfirm.dta" , nogen
replace atfirm=0 if atfirm==. & year<=2019

merge 1:1 lopnr date 	using "$datapath/otherfirm.dta" , nogen
replace otherfirm=0 if otherfirm==. & year<=2019

*Sanity check
assert  atfirm==0 if working==0
assert  otherfirm==0 if working==0



*drop old work variable as it is the same as the wokring variable
sum work if working==1 	// OK!
drop work
}
*

{ // Unemployment 
*===============================================================================
*Note: outcomes generated in A1_clean_sokatper.do

*Days of unemployment per month
merge m:1 persid date 	using "$datapath/unemployment_by_month.dta", keepusing(daysunemp)
drop if _merge==2
drop 	_merge

*Note that unemployment data spans until end of 30dec2015 so no if year<=2013 is needed
replace daysunemp=0 if daysunemp==. 


*Days of unemployment per year
merge m:1 persid year 	using "$datapath/unemployment_by_year.dta"
drop if _merge==2
drop 	_merge

*Note that unemployment data spans until end of 30dec2015 so no if year<=2013 is needed
replace annual_daysunemp=0 if annual_daysunemp==. 

}
*

{ // Earnings and wages outcomes based on RAMS
*===============================================================================
* Note: These are created in A2_gen_earnings.do. 

// ANNUAL earnings and annual wages 	AT ALL FRIMS (w3)
merge m:1 lopnr year 	using "$datapath/annual_ear.dta"		, nogen	
*Already set to zero for years with missing observation

// ANNUAL earnings and annual wages 	AT NOTIFYING FRIM (w1)
merge m:1 lopnr year 	using "$datapath/annual_ear_atfirm.dta"		, nogen
count if annual_ear_atfirm==. &   atfirm==1 	//OK
replace annual_ear_atfirm=0 if annual_ear_atfirm==.

// ANNUAL earnings and annual wages 	AT OTHER FRIM(S) (w2)
merge m:1 lopnr year 	using "$datapath/annual_ear_otherfirm.dta"	, nogen
replace annual_ear_otherfirm=0 if annual_ear_otherfirm==.


// Monthly wages 			AT NOTIFYING FRIM (w1)
merge m:1 lopnr date 	using "$datapath/monhtly_w1.dta"		, nogen

// Monthly wages 			AT OTHER FRIM(S) (w2)
merge m:1 lopnr date 	using "$datapath/monhtly_w2.dta"		, nogen

}
*

{ // Wage survey outcomes
*===============================================================================
merge m:1 persid year	using "$datapath/A1_clean_wagesurvey_00_19_persidyear.dta"
drop if _merge==2
drop 	_merge

}
*


}
*

{ // Merge on firm characteristics
*===============================================================================
*Note: 	Merging on firm charaterisics created from RAMS data in A1_clean_firmchar.do
*	Characterisics are only created for the years 2004-2018 so _merge==1
*	is OK for year 1990-2003.
*	_merge==2 observations comes from the master data being restricted to
*	indivuals who are  getting notified.

merge m:1 firmid year using "$datapath/A1_firm_char.dta"

*Firms are non existent in the year of notification (_merge==1)
tab year  _merge if eventtime_def==0


*NOTE!	_merge==1 observations are observations where displacment data and RAMS
*	data do not match. The firm has gone bankrupt in most cases (see tab below)
tab varselorsak _merge if eventtime_def==0
* The indivuals are never registered as working at the firm
sum atfirm if _merge==1 & eventtime_def==0 

* This implies that the notification date is wrong as the firm is non-exsistent
* in the RAMS data at the year of notiication!

gen missingfirminRAMS_def = _merge==1 & eventtime_def==0


drop if _merge==2
drop 	_merge
}
*
{ // Dropping time periods that are not of use us  (save disk space)
*===============================================================================
*Note: 	In order to keep down the size of the data I drop observations prior to
*	the year 2000. The first notification is in 2005 which impllies that we
* 	have at least 5 years of data before the first notification.

drop if year<2000
drop if year==. // drops pre 2000 obs comming from mergred datasets (otheremployer, atfirm, etc)

}
*	

order 	lopnr date persid varselid firmid eventtime_def working atfirm  

compress
save "$datapath/A2_cleaned_masterdata.dta",replace

 log close
